package com.lzx.iteam;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.lzx.iteam.adapter.DraftAdapter;
import com.lzx.iteam.bean.EventListData;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.AllDialogUtil;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 *  cao  gao xiang
 *   草   稿   箱
 * Created by mac on 15-7-3.
 */
public class DraftsActivity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{

    private LinearLayout mBack,mLlHelp;
    private ListView mDraftList;
    private Activity mActivity;
    private CloudDBOperation mCloudDBOperation;
    private PreferenceUtil mPreferenceUtil;
    private DraftAdapter mAdapter;
    private List<EventListData> mEventData;

    public static final String DRAFT_EVENT_ID = "draft_event_id";
    public static final String  DRAFT_EVENT_UNREAD = "draft_event_unread";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drafts_layout);
        initProperty();
        initView();

    }

    public void initProperty(){
        mActivity = this;
        mCloudDBOperation = new CloudDBOperation(mActivity);
        mEventData = new ArrayList<EventListData>();
        mPreferenceUtil = PreferenceUtil.getInstance(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new AsyncTask<String,Void,List<EventListData>>(){

            @Override
            protected List<EventListData> doInBackground(String... strings) {
                String uid = mPreferenceUtil.getString(PreferenceUtil.CLIENT_USEID, "");
                mEventData = mCloudDBOperation.findAllEventsDrafts(uid);
                return mEventData;
            }

            @Override
            protected void onPostExecute(List<EventListData> EventListDatas) {
                super.onPostExecute(EventListDatas);
                mAdapter = new DraftAdapter(mEventData,mActivity);
                mDraftList.setAdapter(mAdapter);
            }
        }.execute();

        mPreferenceUtil.save(DRAFT_EVENT_UNREAD,0);
    }

    public void initView(){
        mBack = (LinearLayout) findViewById(R.id.draft_ll_back);
        mLlHelp=(LinearLayout)findViewById(R.id.draft_ll_help);
        mBack.setOnClickListener(this);
        mLlHelp.setOnClickListener(this);
        mDraftList = (ListView) findViewById(R.id.draft_lv_list);
        mDraftList.setOnItemClickListener(this);
        mDraftList.setOnItemLongClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.draft_ll_back:
                finish();
                break;
            case R.id.draft_ll_help:
                Intent help = new Intent(mActivity, WebViewActivity.class);
                help.putExtra("title", "如何使用草稿箱?");
                help.putExtra("type", 15);
                startActivity(help);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        EventListData customModelData = mEventData.get(i);
        Intent intent = new Intent(mActivity, CreateNewEventActivity.class);
        Bundle bundle1 = new Bundle();
        if (StringUtil.isEmpty(customModelData.getReceiver())) {
            customModelData.setReceiver("");
        }
        bundle1.putSerializable("event_bundle",customModelData);
        intent.putExtra("event_msg", bundle1);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        EventListData customModelData = mEventData.get(i);
        showDeleteDraftDlg(customModelData.getEventId(),i);
        return true;
    }

    /**
     * 删除草稿
     */
    public void showDeleteDraftDlg(final String eventId , final int position) {
        AllDialogUtil dialogUtil = AllDialogUtil.getInstance(mActivity);
        String okText = this.getString(R.string.label_btn_ok);
        String cancelText = this.getString(R.string.cancel);
        dialogUtil.titleDeleteMsgStyle(okText, cancelText, "删除草稿");
        dialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {
            @Override
            public void onConfirmClick(Dialog lDialog) {
                mEventData.remove(position);
                mAdapter.notifyDataSetChanged();
                mCloudDBOperation.deleteEventDraftsByEventId(eventId);
                lDialog.dismiss();
            }

            @Override
            public void onCancelClick(Dialog lDialog) {
                lDialog.dismiss();
            }

        });

    }
}
